package com.enjoyor.soa.traffic.restt.tpcms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.enjoyor.soa.traffic.api.dto.tpcms.PassCheckDto;
import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.restt.tpcms.dubbo.IPassCheckConsumeService;
import com.enjoyor.soa.traffic.restt.tpcms.dubbo.invoke.UbmsDeptBayonetServiceInvoke;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;
import com.enjoyor.soa.traffic.frame.security.bean.dto.UserDto;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Auth
@RestController
@RequestMapping({ "/passCheck" })
@Api("申请审核Controller页面请求")
@CrossOrigin
public class PassCheckController {

    @Autowired
    private IPassCheckConsumeService passCheckConsumeService;

    @Autowired
    private UbmsDeptBayonetServiceInvoke ubmsDeptService;
    /**
	 * 申请单审核
	 * @param passCheckDto 审核实体： checkStatusId审核通过APPLYSTATUS03，审核不通过APPLYSTATUS04
	 */
	@PostMapping(value = { "/insertPassCheck" }, consumes = { "application/json; charset=UTF-8" }, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("申请单审核")
	public ResultPojo insertPassCheck(@RequestBody PassCheckDto passCheckDto, HttpServletRequest request) {
		return passCheckConsumeService.insertPassCheck(passCheckDto, ((UserDto) request.getAttribute("currentUser")).getPersonName());
	}
	
	/**
	 * 查询审核记录列表信息
	 * @param checkId 审核编号
	 * @param passApplyId 申请编号
	 * @param checkPersonId 审核人编号
	 * @param checkDeptId 审核部门编号
	 * @param checkStartTime 审核开始时间(yyyy-mm-dd hh24:mi:ss)
	 * @param checkEndTime 审核结束时间(yyyy-mm-dd hh24:mi:ss)
	 * @param checkStatusId 审核状态编号
	 * @return
	 */
    @GetMapping(value = { "/getPassCheckList" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "获取审核信息", notes = "查询数据库中符合条件的审核数据")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "passApplyId", value = "申请编号", paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "checkId", value = "审核编号", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "checkPersonId", value = "审核人编号", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "checkDeptId", value = "审核部门编号", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "checkStartTime", value = "审核开始时间", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "checkEndTime", value = "审核结束时间", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "checkStatusId", value = "审核状态编号", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "isDelete", value = "是否删除（是1，否0）", paramType = "query", dataType = "String") })
	public ResultPojo getPassCheckList(
			@RequestParam(defaultValue = "") String checkId, 
			@RequestParam(defaultValue = "") String passApplyId, 
			@RequestParam(defaultValue = "") String checkPersonId,
			@RequestParam(defaultValue = "") String checkDeptId,  
			@RequestParam(defaultValue = "") String checkStartTime,
			@RequestParam(defaultValue = "") String checkEndTime, 
			@RequestParam(defaultValue = "") String checkStatusId, 
			@RequestParam(defaultValue = "") String isDelete, 
			HttpServletRequest request) {
        return passCheckConsumeService.getPassCheckList(checkId, passApplyId, checkPersonId,
                checkDeptId, checkStartTime, checkEndTime, checkStatusId, isDelete);
    }
    
    /**
	 * 查询申请单分页信息-审核模块
	 * @param page 分页参数
	 * @return
	 */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @GetMapping(value = { "/getPassApplyCheckPage" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "申请单分页查询", notes = "申请单分页查询")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "passApplyId", value = "申请编号（模糊查询）", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "applyPeople", value = "申请人（模糊查询）", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "applyDept", value = "所属单位（模糊查询）", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "applyTypeId", value = "申请种类", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "applyStartTime", value = "申请开始时间", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "applyEndTime", value = "申请结束时间", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "applyStatusId", value = "申请状态编号", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "licenseNumber", value = "车牌号码", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "dataDept", value = "数据权限部门", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "dataRole", value = "数据权限角色", paramType = "query", dataType = "String") })
    public ResultPojo getPassApplyCheckPage(
    		@RequestParam(defaultValue = "") String passApplyId, 
    		@RequestParam(defaultValue = "") String applyPeople,
			@RequestParam(defaultValue = "") String applyDept, 
			@RequestParam(defaultValue = "") String applyTypeId, 
			@RequestParam(defaultValue = "") String applyStartTime,
			@RequestParam(defaultValue = "") String applyEndTime, 
			@RequestParam(defaultValue = "") String applyStatusId,
			@RequestParam(defaultValue = "") String licenseNumber,
			@RequestParam(defaultValue = "") String dataDept, 
            @RequestParam(defaultValue = "") String dataRole,
    		Page page, HttpServletRequest request) {
    	Map<String,Object> mapBean = new HashMap<String,Object>();
    	mapBean.put("passApplyId", passApplyId);
    	mapBean.put("applyPeople", applyPeople);
    	mapBean.put("applyDept", applyDept);
    	mapBean.put("applyTypeId", applyTypeId);
    	mapBean.put("applyStartTime", applyStartTime);
    	mapBean.put("applyEndTime", applyEndTime);
    	mapBean.put("applyStatusId", applyStatusId);
    	mapBean.put("licenseNumber", licenseNumber);
    	if(null != dataDept && dataDept != "") {
        	if("department".equals(dataRole) || "leader".equals(dataRole)) {
            	
            }else {
            	List<String> dataDeptIds = ubmsDeptService.getDeptsByParentId(dataDept);
                mapBean.put("dataDeptIds", dataDeptIds);
            }
        }
    	//不显示待提交
    	mapBean.put("removeStatusId", "APPLYSTATUS01");
    	page.setMapBean(mapBean);
        return passCheckConsumeService.getPassApplyCheckPage(page);
    }
}
